# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: leon

from absl import app, flags

import pandas
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

FLAGS = flags.FLAGS


def plot_histogram(book_df, name):
  plt.figure(figsize=(10, 10))
  plt.title('Binance futures max ft-et (%s)' % name)
  plt.hist(book_df['max_message_time_diff'], bins=50)
  #plt.yscale('log', basey=2)
  plt.savefig('hist/hist_timediff_%s.png' % name)

  plt.figure(figsize=(10, 10))
  plt.title('Binance futures ft-et stddev (%s)' % name)
  plt.hist(book_df['stddev_message_time_diff'], bins=50)
  #plt.yscale('log', basey=2)
  plt.savefig('hist/hist_stddev_%s.png' % name)


def read_csv_into_df(csv_path):
  df = pandas.read_csv(csv_path, sep=',', header=0)
  return df


def print_stats(merged_df):
  percentiles = [.1, .3, .5, .7, .9, .95, .99]
  pandas.set_option('display.max_columns', 100)
  pandas.set_option('display.width', 1000)
  print(merged_df.groupby(['market_type', 'exchange', 'api_version', ], as_index=False).
      describe(percentiles=percentiles))


def merge_and_diff_and_avg(book_df1, book_df2):
  compare_cols = [
      'exchange_api_id',
      'recipe',
      'symbol',
      'feed_source',
      'market_type',
      'exchange',
      'api_version',
  ]

  merged_df = pandas.merge(book_df1, book_df2, on=compare_cols, how='inner')
  merged_df['letency_diff'] = merged_df['max_message_time_diff_x'] - merged_df['max_message_time_diff_y']

  care_cols = [
      'market_type',
      'exchange',
      'api_version',
      'letency_diff',

  ]
  merged_df = merged_df[care_cols]
  avg_df = merged_df.groupby(['market_type', 'exchange', 'api_version',], as_index=False).mean()
  print(avg_df)
  return merged_df


def main(argv):
  csv_path1 = FLAGS.csv_path1
  assert csv_path1, '--csv_dir1 must be specified.'
  csv_path2 = FLAGS.csv_path2
  assert csv_path2, '--csv_dir2 must be specified.'

  book_df1 = read_csv_into_df(csv_path1)
  book_df2 = read_csv_into_df(csv_path2)

  plot_histogram(book_df1, '5 symbols per connection')
  plot_histogram(book_df2, '10 symbols per connection')

  merged_df = merge_and_diff_and_avg(book_df1, book_df2)
  print_stats(merged_df)

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('csv_path1', None, 'Intput path1.')

  flags.DEFINE_string('csv_path2', None, 'Intput path2.')

  app.run(main)
